package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.payconfig.EntityFkParamConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.FKParamConfigRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.FkParamConfigService;
import com.ocom.payconfig.service.XfDeviceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class FKParamConfigController {


    @Autowired
    private FkParamConfigService fkParamConfigService;

    @Autowired
    private XfDeviceInfoService xfDeviceInfoService;


    /*
     * @Description:2.33.获取商户风控参数 （getFkConfig）
     * @Author: lu 
     * @Date: 2022/7/7 10:38
     * @param comId: 
     * @param merchId:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(PayConfigUrl.Url_getFkConfig)
    public Result getFkConfig(Long comId,Integer merchId){
        try {
            if (comId==null || merchId==null){
                return ResultUtil.error(ResultEnum.Err_203);
            }
            Map<String,Object> map=new HashMap<>();
            EntityFkParamConfig entityFkParamConfig=fkParamConfigService.selectFKParamConfigList(comId,merchId);
            if (entityFkParamConfig==null){
                return ResultUtil.success();
            }else {
                map.put("comId",comId);
                map.put("merchId",merchId);
                map.put("offlineCanPay",entityFkParamConfig.getIsOffline());
                map.put("offlineTimeSlot",entityFkParamConfig.getMaxtimeOffline());
                map.put("offlineOrderNum",entityFkParamConfig.getAlltimesOffline());
                map.put("offlineAmount",entityFkParamConfig.getAllamountOffline());
                map.put("userOrderNum",entityFkParamConfig.getOnetimesOffline());
                map.put("userAmount",entityFkParamConfig.getOneamountOfflin());
                map.put("orderAmount",entityFkParamConfig.getMaxamountOfflin());
                map.put("offlineUrl",entityFkParamConfig.getOfflineUrl());
                map.put("refundMode",entityFkParamConfig.getRefundMode());
            }
            return ResultUtil.success(map);
        }catch (Exception e){
            log.info("获取商户风控参数异常,商户id:"+merchId+"异常信息:"+e);
            return ResultUtil.error(ResultEnum.Err_900);
        }
    }

    /*
     * @Description:2.34.设置商户风控参数 （setFkConfig）
     * @Author: lu 
     * @Date: 2022/7/7 11:18
     * @param fkParamConfigRequest:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(PayConfigUrl.Url_setFkConfig)
    public Result setFkConfig(@RequestBody FKParamConfigRequest fkParamConfigRequest){
        try {
            EntityFkParamConfig entityFkParamConfig=fkParamConfigService.selectFKParamConfigList(fkParamConfigRequest.getComId(),fkParamConfigRequest.getMerchId());
            EntityFkParamConfig entityFkParamConfigs=new EntityFkParamConfig();
            if (fkParamConfigRequest.getOfflineCanPay()==0){
                entityFkParamConfigs.setIsOffline((byte) 0);
            }else {
                entityFkParamConfigs.setIsOffline((byte) 1);
            }
            entityFkParamConfigs.setMerchId(fkParamConfigRequest.getMerchId());
            entityFkParamConfigs.setComId(fkParamConfigRequest.getComId());
            entityFkParamConfigs.setMaxtimeOffline(fkParamConfigRequest.getOfflineTimeSlot());
            entityFkParamConfigs.setAlltimesOffline(fkParamConfigRequest.getOfflineOrderNum());
            entityFkParamConfigs.setAllamountOffline(fkParamConfigRequest.getOfflineAmount());
            entityFkParamConfigs.setOnetimesOffline(fkParamConfigRequest.getUserOrderNum());
            entityFkParamConfigs.setOneamountOfflin(fkParamConfigRequest.getUserAmount());
            entityFkParamConfigs.setMaxamountOfflin(fkParamConfigRequest.getOrderAmount());
            entityFkParamConfigs.setUpdateUser(fkParamConfigRequest.getAccount());
            entityFkParamConfigs.setOfflineUrl(fkParamConfigRequest.getOfflineUrl());
            entityFkParamConfigs.setRefundMode(fkParamConfigRequest.getRefundMode());
            if (entityFkParamConfig==null){
                int i=fkParamConfigService.saveFkParamConfig(fkParamConfigRequest.getComId(),entityFkParamConfigs);
                if (i<=0){
                    return ResultUtil.error(ResultEnum.Err_2016);
                }
            }else {
                int i=fkParamConfigService.updateFkParamConfig(fkParamConfigRequest.getComId(),entityFkParamConfigs);
                if (i<=0){
                    return ResultUtil.error(ResultEnum.Err_3014);
                }
            }
            if (!xfDeviceInfoService.updatePara(fkParamConfigRequest.getComId())){
                log.info("风控参数修改下传表示失败,商户id:"+fkParamConfigRequest.getMerchId());
            }
            return ResultUtil.success();
        }catch (Exception e){
            log.info("设置商户风控参数异常,商户id:"+fkParamConfigRequest.getMerchId()+"异常信息:"+e);
            return ResultUtil.error(ResultEnum.Err_900);
        }
    }

    /*
     * @Description:删除商户风控参数
     * @Author: lu 
     * @Date: 2022/7/14 13:55
     * @param companyId: 
     * @param merchId:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(PayConfigUrl.Url_deleteFkConfig)
    public Result deleteFkConfig(Long companyId, Integer merchId){
        if (companyId==null || merchId==null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            int i=fkParamConfigService.deleteFkConfig(companyId,merchId);
            if (i<=0){
                return ResultUtil.error(ResultEnum.Err_3016);
            }
            if (!xfDeviceInfoService.updatePara(companyId)){
                log.info("风控参数修改下传表示失败,商户id:"+merchId);
            }
            return ResultUtil.success();
        }catch (Exception e){
            log.info("删除商户风控参数异常,商户id:"+merchId+"异常信息:"+e);
            return ResultUtil.error(ResultEnum.Err_900);
        }
    }
}
